package com.zzx.magic.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Collections;

/**
 * 代码生成工具
 *
 * @Author Zhangxx
 * @Date 2024/6/11 14:57
 */
public class CodeGenerationUtils {

    /**
     * 需要生成的表名
     */
    private static final String[]   TABLE_NAMES = new String[]{"bp_feedback","bp_interested","bp_recommend","business_plan"};

    /**
     * 文件路径
     */
    public static final String PROJECT_PATH = "C:\\Users\\19808\\Desktop\\";

    /**
     * 项目名
     */
    public static final String PROJECT_NAME = "magic";

    /**
     * 数据源配置
     */
    public static final String DATA_SOURCE_URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
    public static final String DATA_SOURCE_USERNAME = "root";
    public static final String DATA_SOURCE_PASSWORD = "root";
    public static final String DATA_SOURCE_DRIVER = "com.mysql.cj.jdbc.Driver";

    public static void main(String[] args) {

        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setDataSource(getDataSourceConfigInfo());
        autoGenerator.setGlobalConfig(getGlobalConfigInfo());
        autoGenerator.setPackageInfo(getPackageConfigInfo());
        autoGenerator.setStrategy(getStrategyConfigInfo(TABLE_NAMES));

        autoGenerator.execute();
    }

    /**
     * 策略配置
     *
     * @param tableNames
     * @return
     */
    private static StrategyConfig getStrategyConfigInfo(String[] tableNames) {
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setInclude(tableNames);
        strategyConfig.setLogicDeleteFieldName("del_flag");
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setTablePrefix("");
        strategyConfig.setRestControllerStyle(true);
        return strategyConfig;
    }

    /**
     * 包配置
     *
     * @return
     */
    private static PackageConfig getPackageConfigInfo() {
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.zzx.magic");
//        packageConfig.setModuleName(null);
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setController("controller");
        return packageConfig;
    }

    private static GlobalConfig getGlobalConfigInfo() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(PROJECT_PATH + PROJECT_NAME + "/src/main/java");

        globalConfig.setAuthor("ZhangXx");
        globalConfig.setOpen(false);
        globalConfig.setSwagger2(true);
        globalConfig.setBaseColumnList(true);
        globalConfig.setBaseResultMap(true);
//        globalConfig.setActiveRecord(false);
//        globalConfig.setFileOverride(true);
//        globalConfig.setServiceName("%Service");
        return globalConfig;

    }

    private static DataSourceConfig getDataSourceConfigInfo() {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(DATA_SOURCE_URL);
        dataSourceConfig.setUsername(DATA_SOURCE_USERNAME);
        dataSourceConfig.setPassword(DATA_SOURCE_PASSWORD);
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName(DATA_SOURCE_DRIVER);
        return dataSourceConfig;
    }


}